package com.shxc.smarttrade.old.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.shxc.smarttrade.old.utils.CloseAllActivity;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;
import com.shxc.smarttrade.old.utils.SpUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/6/7.
 */

public class BindBankActivity extends BaseActivity {
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_indentity_num)
    TextView tvIndentityNum;
    @BindView(R.id.et_bank_name)
    EditText etBankName;
    @BindView(R.id.et_card_num)
    EditText etCardNum;
    @BindView(R.id.et_provice)
    EditText etProvice;
    @BindView(R.id.et_city)
    EditText etCity;
    @BindView(R.id.et_detail_address)
    EditText etDetailAddress;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    String[] strData = (String[]) msg.obj;
                    tvIndentityNum.setText(strData[0]);
                    tvName.setText(strData[1]);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_bank);
        CloseAllActivity.getScreenManager().pushActivity(this);
        initView();
        showShortDialog();
        initData(SpUtils.getInt(this, "userId"));
    }

    /**
     * 初始化View
     */
    private void initView() {
        tvCenter.setText("绑定银行卡");
    }

    /**
     * 初始化数据
     */
    private void initData(int userId) {

        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", SpUtils.getString(this, "accessToken", ""));
        map.put("userId", userId + "");
        MyOkHttpUtils.getServiceDatas(this,map, URLContents.BIND_BANK_ACTIVITY_CARD_INDEX, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                dismissShortDialog();
                showToast(R.string.net_error);
            }

            @Override
            public void success(String response, int id) {
                if (response != null) {
                    JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                    String errCode = "";
                    String errMsg = "";
                    if (!jsonObject.get("err_code").isJsonNull())
                        errCode = jsonObject.get("err_code").getAsString();
                    if (!jsonObject.get("err_msg").isJsonNull())
                        errMsg = jsonObject.get("err_msg").getAsString();
                    if (errCode.equals("0")) {

                        if (!jsonObject.get("data").isJsonNull()) {
                            JsonObject data = jsonObject.get("data").getAsJsonObject();
                            String identity = "";
                            int userId = -1;
                            String userName = "";
                            if (!data.get("identity").isJsonNull())
                                identity = data.get("identity").getAsString();
                            if (!data.get("userId").isJsonNull())
                                userId = data.get("userId").getAsInt();
                            if (!data.get("userName").isJsonNull())
                                userName = data.get("userName").getAsString();
                            String[] strData = {identity, userName};
                            Message msg = Message.obtain();
                            msg.what = 0;
                            msg.obj = strData;
                            handler.sendMessage(msg);

                        } else {
                            showToast(R.string.data_error);
                        }

                    } else {
                        showToast(R.string.data_error);
                    }
                }

                dismissShortDialog();
            }
        });
    }

    @OnClick(R.id.rl_left)
    public void onRlLeftClicked() {
        finish();
    }

    @OnClick(R.id.btn_sure)
    public void onBtnSureClicked() {
        String bankName = etBankName.getText().toString();
        String cardNum = etCardNum.getText().toString();
        String provice = etProvice.getText().toString();
        String city = etCity.getText().toString();
        String detailAddress = etDetailAddress.getText().toString();
        String name[] = {"银行", "卡号", "开户省份", "开户城市", "开户地址"};
        String data[] = {bankName, cardNum, provice, city, detailAddress};
        String result = BaseUtils.getNullString(name, data);
        if (result == null) {
            showShortDialog();
            bindBank(SpUtils.getInt(this, "userId"), bankName, cardNum, provice, city, detailAddress);
        } else {
            showToast(result + " 不能为空");
        }

    }

    /**
     * 绑定银行卡
     *
     * @param userId
     * @param bankName
     * @param cardNo
     * @param province
     * @param city
     * @param bankAddr
     */
    private void bindBank(int userId, String bankName, String cardNo, String province, String city, String bankAddr) {

        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", SpUtils.getString(this, "accessToken", ""));
        map.put("userId", userId + "");
        map.put("bankName", bankName);
        map.put("cardNo", cardNo);
        map.put("province", province);
        map.put("city", city);
        map.put("bankAddr", bankAddr);
        MyOkHttpUtils.getServiceDatas(this,map, URLContents.BIND_BANK_ACTIVITY_CARD_INSERT, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                showToast(R.string.net_error);
                dismissShortDialog();
            }

            @Override
            public void success(String response, int id) {
                if (response != null) {
                    JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                    String errCode = "";
                    String errMsg = "";
                    if (!jsonObject.get("err_code").isJsonNull())
                        errCode = jsonObject.get("err_code").getAsString();
                    if (!jsonObject.get("err_msg").isJsonNull())
                        errMsg = jsonObject.get("err_msg").getAsString();
                    if (errCode.equals("0")) {
                        showToast("绑定成功");
                        finish();
                    } else {
                        showToast(errMsg);
                    }

                } else {
                    showToast(R.string.data_error);
                }
                dismissShortDialog();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CloseAllActivity.getScreenManager().popActivity(this);

    }
}
